package erp

import (
	"context"
	"encoding/json"
	"ez/apps/erp/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type BuyOrderProduct struct {
	mgo.BaseDoc  `bson:"-" json:"-"`
	Id           int64      `json:"id" bson:"id,omitempty"`
	BelongTo     *BuyOrder  `json:"belongTo" bson:"belongTo"`         //所属
	BelongToId   int64      `json:"belongToId" bson:"belongToId"`     //所属
	Src          int64      `json:"src" bson:"src"`                   //源单明细ID
	SrcSN        string     `json:"srcSN" bson:"srcSN"`               //源单单号
	Product      *Product   `json:"product" bson:"product"`           //产品
	ProductId    int64      `json:"productId" bson:"productId"`       //产品
	Num          float64    `json:"num" bson:"num"`                   //数量
	Unit         *Unit      `json:"unit" bson:"unit"`                 //单位
	UnitId       int64      `json:"unitId" bson:"unitId"`             //单位
	Price        float64    `json:"price" bson:"price"`               //含税单价
	Money        float64    `json:"money" bson:"money"`               //含税总价
	Cut          float64    `json:"cut" bson:"cut"`                   //优惠金额
	CutRate      float64    `json:"cutRate" bson:"cutRate"`           //折扣率
	Tax          float64    `json:"tax" bson:"tax"`                   //税额
	TaxRate      *TaxRate   `json:"taxRate" bson:"taxRate"`           //税率
	TaxRateId    int64      `json:"taxRateId" bson:"taxRateId"`       //税率
	PriceRef     float64    `json:"priceRef" bson:"priceRef"`         //参考单价
	MoneyRef     float64    `json:"moneyRef" bson:"moneyRef"`         //参考总价
	PriceConfirm float64    `json:"priceConfirm" bson:"priceConfirm"` //评审单价
	MoneyConfirm float64    `json:"moneyConfirm" bson:"moneyConfirm"` //评审总价
	Note         string     `json:"note" bson:"note"`                 //备注
	Row          int64      `json:"row" bson:"row"`                   //RN
	CreateAt     time.Time  `json:"createAt" bson:"createAt"`
	UpdateAt     time.Time  `json:"updateAt" bson:"updateAt"`
	DeleteAt     *time.Time `json:"deleteAt" bson:"deleteAt"`
}

func (this *BuyOrderProduct) DocName() string { return "BuyOrderProduct" }
func (this *BuyOrderProduct) GetId() int64    { return this.Id }
func (this *BuyOrderProduct) SetId(id int64)  { this.Id = id }
func (this *BuyOrderProduct) Create() error {
	return this.GetFactory().Create(this)
}
func (this *BuyOrderProduct) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *BuyOrderProduct) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *BuyOrderProduct) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *BuyOrderProduct) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *BuyOrderProduct) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *BuyOrderProduct) ToString() string {
	return string(this.ToBytes())
}
func (this *BuyOrderProduct) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *BuyOrderProduct) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *BuyOrderProduct) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *BuyOrderProduct) LoadBelongTo() {
	if this.BelongToId == 0 {
		return
	}
	this.BelongTo, _ = NewBuyOrderCrud().FindId(this.BelongToId)
}
func (this *BuyOrderProduct) LoadProduct() {
	if this.ProductId == 0 {
		return
	}
	this.Product, _ = NewProductCrud().FindId(this.ProductId)
}
func (this *BuyOrderProduct) LoadUnit() {
	if this.UnitId == 0 {
		return
	}
	this.Unit, _ = NewUnitCrud().FindId(this.UnitId)
}
func (this *BuyOrderProduct) LoadTaxRate() {
	if this.TaxRateId == 0 {
		return
	}
	this.TaxRate, _ = NewTaxRateCrud().FindId(this.TaxRateId)
}
func (this *BuyOrderProduct) ClearRelationsBeforeSave() mgo.Doc {
	this.BelongTo = nil
	this.Product = nil
	this.Unit = nil
	this.TaxRate = nil
	return this
}
func neverUsed_BuyOrderProduct() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type BuyOrderProductAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *BuyOrderProduct
	Session *ez.Session
}

func NewBuyOrderProductAccessControl(model *BuyOrderProduct, action string, session *ez.Session) *BuyOrderProductAccessControl {
	ctrl := &BuyOrderProductAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.BuyOrderProductAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
